<?php

namespace App\Models;

use Session;
use Cookie;

class Admin extends BaseModel
{
    protected $table = "admin";

    const PREFIX = 'ag_';
    const SALT = "whydyyeeyryereu**((((541";


    public function __construct()
    {
        parent::__construct();
$this->fields = ['id', 'wxname', 'wxavatar', 'cardnum','openid','invit', 'reffer','fd', 'rid', 'role', 'status', 'point', 'piao'];
}

    /**
     * 按条件查询单条数据
     */
    public function getOne(array $where, $fields = '*')
    {
        return $this->multiSelect($fields)->multiWhere($where)->first();
    }

    /**
     * 按条件查询全部数据,根据配置显示条数显示
     */
    public function getList(array $where = [], $fields = '*', $order = '', $pageSize = '')
    {
        if ($pageSize) {
            return $this->multiSelect($fields)->multiWhere($where)->multiOrder($order)->paginate($pageSize);
        } else {
            return $this->multiSelect($fields)->multiWhere($where)->multiOrder($order)->get();
        }
    }

    /**
     * 根据条件删除数据
     */
    public function del(array $where)
    {
        return $this->multiWhere($where)->delete();
    }

    /**
     *插入数据
     */
    public function add($data)
    {
//        dd($data);
        return $this->insertGetId($data);
    }

    /**
     *更新数据
     */
    public function up($where, $data)
    {
        return $this->multiWhere($where)->update($data);
    }

    /**
     * 获取和
     */
    public function getSum($where, $field)
    {
        return $this->multiWhere($where)->sum($field);
    }

    /**
     * 字段递加
     */
    public function inc($where, $field, $n)
    {
        return $this->multiWhere($where)->increment($field, $n);
    }

    public function getImageData(){
        return $this->getOne(array('id'=>session('adminId')),'proxy');
    }


    public function getDownImage(){
//        $data = $this->getImageData();
//        $filename = $data['proxy'];

        $filename = './image/zhengshu.png';
        $name = basename($filename);
        $file  =  fopen($filename, "rb");
        Header( "Content-type:  application/octet-stream ");
        Header( "Accept-Ranges:  bytes ");
        Header( "Content-Disposition:  attachment;  filename= ".$name);

        $contents = "";
        while (!feof($file)) {
            $contents .= fread($file, 8192);
        }
        echo $contents;
        fclose($file);
    }

        //上传样板图
    public function uploadImage($request)
    {
        $aid = session('adminId');
        $path = '';
        if($request['img']){

            $base64_url = request('img');

            $base64_body = substr(strstr($base64_url,','),1);

            $pics= base64_decode($base64_body);

            $path = 'image/zhengshu.png';

            $data['proxy'] = $path;

            $where['id'] = $aid;
            $res = $this->up($where,$data);
            if(!$res){
                return ['status'=>0,'msg'=>"品牌代理样板上传失败1"];
            }

            if(file_put_contents($path,$pics)){

                return ['status'=>1,'msg'=>'品牌代理样板上传成功','data'=>$path];

            }else{

                return ['status'=>0,'msg'=>'品牌代理书样板失败2','data'=>$path];

            }


        }

        return ['status'=>1,'msg'=>'图片上传失败','data'=>$path];

    }

    /**
 * 登录
 * @param $request 参数
 * @return array
 */
    public function login($request)
    {
        $account = $request['username'];
        $password = $request['password'];
        $var_code = $request['code'] ;
        if(empty($account)||empty($password)){
            $res = ['status'=>0,'msg'=>"登录信息不能为空"];
            return $res;
        }
        // 登录验证码
//        if(!check_captcha($var_code)){
//            $res = ['status'=>0,'msg'=>"验证码错误"];
//            return $res;
//        }

        if(!empty($var_code)){

            if(strtolower($var_code)!=strtolower(session('code'))){

                $res = ['status'=>0,'msg'=>"验证码错误"];
                return $res;

            }


        }else{

            $res = ['status'=>0,'msg'=>"验证码不能为空"];
            return $res;

        }


        $where['username'] = $account;
        $where['password'] = MD5($password) ;
        $brand = new Brand();
        $result = $brand->getOne($where);
        //dd($result);
        $gid = 0;

        if(!$result){
            $result = $this->getOne($where);
            if(!$result){
                $condition['mobile'] = $account;
                $condition['password'] = MD5($password) ;
                $result = $brand->getOne($condition);
                if (!$result) {
                    $res = ['status'=>0,'msg'=>'用户名或密码错误!'];
                    return $res;
                }
            } else {
                $gid = 1;
            }
        }
        if ($gid != 1 && $result['status'] == 0) {
            return ['status'=>0,'msg'=>'该账号正在审核中，通过后方可登录!'];
        }
        if ($gid != 1 && $result['status'] == 2) {
            return ['status'=>0,'msg'=>'该账号审核失败，无法登录!'];
        }
         //dd(123);
        $res = ['status'=>1,'msg'=>'登录成功','type'=>$gid];
        //addSession("adminId",$resulte['id']);
        //addSession("adminType",$gid);
        //addSession("adminname",$resulte['username']);
        session(['adminId'=>$result['id']]);
        session(["adminType"=>$gid]);
        session(["adminname"=>$result['username']]);
        return $res;
    }


}